import { accessTypes } from "../constants/enums";

export const getAccessName = (type) => {
  switch (type) {
    case accessTypes.UPDATE: {
      return "Update";
    }
    case accessTypes.CREATE: {
      return "Create";
    }
    case accessTypes.DELETE: {
      return "Delete";
    }
    case accessTypes.READ:
    default: {
      return "Read";
    }
  }
};

export const hasAccess = (privileges, entityName, accessType) => {
  let result = false;
  if (
    privileges !== undefined &&
    privileges !== null &&
    privileges.length > 0
  ) {
    let found = privileges.find((x) => x.entityName === entityName);
    if (found !== null && found !== undefined) {
      switch (accessType) {
        case accessTypes.CREATE: {
          result = found.canCreate === true;
          break;
        }
        case accessTypes.UPDATE: {
          result = found.canUpdate === true;
          break;
        }
        case accessTypes.DELETE: {
          result = found.canDelete === true;
          break;
        }
        default:
        case accessTypes.READ: {
          result = found.canRead === true;
          break;
        }
      }
    }
  }
  return result;
};



